import Vue from "vue";
import VueRouter from "vue-router";
import Index from "@/views/index.vue";
import Article from "@/views/article.vue";
import Contact from "@/views/contact.vue";
import UserInfo from "@/views/userInfo.vue";
import ArticleDetail from "@/views/articleDetail.vue";
import store from "@/store/index";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/index",
    name: "index",
    component: Index,
  },
  {
    path: "/article",
    name: "article",
    component: Article,
  },
  {
    path: "/articleDetail",
    name: "articleDetail",
    component: ArticleDetail,
  },
  {
    path: "/contact",
    name: "contact",
    component: Contact,
  },
  {
    path: "/userInfo",
    name: "userInfo",
    component: UserInfo,
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.name.includes("article")) {
    store.commit("updateActiveMenuName", "article");
    next();
    return;
  }
  store.commit("updateActiveMenuName", to.name);
  next();

  console.log(to, from);
});

export default router;
